﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows;
using OODCRM.Common;
using System.Data.Linq;

namespace OODCRM
{
    public partial class MainWindow : RibbonWindow
    {

        private void btnCat_tabThemCoHoiDialog_Click(object sender, RoutedEventArgs e)
        {
            switch (dicDialogMode[CRMDialog.ThemCoHoi])
            {
                case DialogMode.Them:
                    {
                        if (dicDialogData.ContainsKey(CRMDialog.ThemCoHoi)
                            && dicDialogData[CRMDialog.ThemCoHoi] != null)
                        {
                            var item = dicDialogData[CRMDialog.ThemCoHoi] as COHOI;
                            if (item == null) break;

                            // update du lieu ko bind dc                           
                            var chusohuu = cbxChuSoHuu_tabThemCoHoiDialog.SelectedItem as User;
                            if (chusohuu != null)
                                item.DOITUONG.User = chusohuu;

                            // cap nhat du lieu
                            try
                            {
                                item.Loai = cbxLoai_tabThemCoHoiDialog.SelectedItem.ToString();
                                dataContext.COHOIs.InsertOnSubmit(item);
                                dataContext.SubmitChanges();

                                if (item != null)
                                    listCoHoi.Add(item);
                            }
                            catch (Exception ex)
                            {
                                //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                                MessageBox.Show(ex.Message);
                                dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                            }
                        }
                    }
                    break;

                case DialogMode.Sua:
                    {
                        var item = dicDialogData[CRMDialog.ThemCoHoi] as COHOI;
                        if (item == null) break;

                        // update du lieu ko bind dc                       
                        var chusohuu = cbxChuSoHuu_tabThemCoHoiDialog.SelectedItem as User;
                        if (chusohuu != null)
                            item.DOITUONG.User = chusohuu;

                        // cap nhat du lieu
                        try
                        {
                            
                            dataContext.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                            MessageBox.Show(ex.Message);
                            dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                        }
                    }
                    break;

                default:
                    break;
            }
            tabCoHoi.IsSelected = true;
        }

        private void btnHuyBo_tabThemCoHoiDialog_Click(object sender, RoutedEventArgs e)
        {
            if (dicDialogMode[CRMDialog.ThemCoHoi] == DialogMode.Sua)
            {
                var item = dicDialogData[CRMDialog.ThemCoHoi] as COHOI;
                if (item != null)
                    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, item);

                int index = listCoHoi.IndexOf(item);
                listCoHoi.RemoveAt(index);
                listCoHoi.Insert(index, item);
            }

            tabCoHoi.IsSelected = true;
        }


        #region Auto

        
        #endregion Auto


    }
}
